package cn.example.vip.mall.goods.controller;

import cn.example.vip.mall.util.RespResult;
import cn.example.vip.mall.goods.entity.CategoryAttr;
import cn.example.vip.mall.goods.service.CategoryAttrService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin	// 生产环境需要去掉
@RestController
@RequestMapping("/categoryAttr")
public class CategoryAttrController {
	@Autowired
	CategoryAttrService categoryAttrService;
	
	
	/**
	 * 添加 分类属性
	 */
	@PostMapping("/add")
	public RespResult add(@RequestBody CategoryAttr category) {
		if(categoryAttrService.save(category)){
			return RespResult.ok();
		}else {
			return RespResult.error();
		}
	}
	
	/**
	 * 根据id 更新 分类属性
	 */
	@PostMapping("/update/{categoryId}/{brandId}")
	public RespResult update(@RequestBody CategoryAttr categoryAttr,
							 @PathVariable("categoryId") Integer categoryId,
							 @PathVariable("brandId") Integer brandId) {
		return RespResult.ok(categoryAttrService.updateByCategory(categoryAttr, categoryId, brandId));
	}
	
	/**
	 * 根据id 删除 分类属性
	 */
	@DeleteMapping("/{categoryId}/{attrId}")
	public RespResult delete(@PathVariable Integer categoryId, @PathVariable Integer attrId) {
		return RespResult.ok(categoryAttrService.deleteById(categoryId, attrId));
	}
	
	/**
	 * 查找 相似的全部分类属性
	 */
	@PostMapping("/search")
	public RespResult<List<CategoryAttr>> search(@RequestBody CategoryAttr category) {
		List<CategoryAttr> categoryList = categoryAttrService.queryList(category);
		return RespResult.ok(categoryList);
	}
	
	/**
	 * 分页查找 相似的全部分类属性
	 */
	@PostMapping("/search/{page}/{size}")
	public RespResult<List<CategoryAttr>> search(@RequestBody CategoryAttr category,
											 @PathVariable Long page,
											 @PathVariable Long size) {
		Page<CategoryAttr> categoryPageList = categoryAttrService.queryPageList(category, page, size);
		return RespResult.ok(categoryPageList);
	}


}
